package com.tarena.lbs.basic.service.impl;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.tarena.lbs.basic.dao.repository.AreaRepository;
import com.tarena.lbs.basic.pojo.po.AreaPO;
import com.tarena.lbs.basic.pojo.vo.AreaVO;
import com.tarena.lbs.basic.service.AreaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@DubboService
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaRepository areaRepository;

    @Override
    public AreaPO selectById(Long adCode){
        return areaRepository.selectById(adCode);
    }

    @Override
    public List<AreaVO> queryProvinces() {
        log.debug("查询省级地区列表业务");
        List<AreaPO> pos = areaRepository.queryProvinces();
        List<AreaVO> vos = convertToVO(pos);
        log.debug("查询省级地区列表业务,vos={}", vos);
        return vos;
    }

    /**
     * 将PO列表转换为VO列表
     * @param pos PO列表
     * @return VO列表
     */
    private List<AreaVO> convertToVO(List<AreaPO> pos){
        List<AreaVO> vos = null;
        if (!CollectionUtils.isEmpty(pos)) {
            vos = new ArrayList<>();
            for (AreaPO po : pos) {
                AreaVO vo = new AreaVO();
                BeanUtils.copyProperties(po, vo);
                vos.add(vo);
            }
        }
        return vos;
    }

    @Override
    public List<AreaVO> queryChildren(Integer parentId) {
        log.debug("查询地区/市区列表业务,parentId={}", parentId);
        List<AreaPO> pos = areaRepository.queryChildren(parentId);
        List<AreaVO> vos = convertToVO(pos);
        log.debug("查询地区/市区列表业务,vos={}", vos);
        return vos;
    }

    @Override
    public List<AreaVO> queryTree() {
        log.debug("查询地市树业务");
        long startTime = System.currentTimeMillis();
        /*
        // 调用 queryChildList() 方法递归查询子地区列表，性能低下
        List<AreaVO> vos = queryProvinces();
        for (AreaVO vo : vos) {
            queryChildList(vo);
        }
        */
        List<AreaPO> allPos = areaRepository.queryAll();
        List<AreaVO> allVos = convertToVO(allPos);
        Map<Integer, AreaVO> vosMap = new HashMap<>();
        for (AreaVO vo : allVos) {
            vosMap.put(vo.getId().intValue(), vo);
        }
        List<AreaVO> areaTree = new ArrayList<>();
        for (AreaVO vo : allVos) {
            if (vo.getParentId().intValue() == 0) {
                areaTree.add(vo);
            } else {
                AreaVO parent = vosMap.get(vo.getParentId().intValue());
                if (parent != null) {
                    if (CollectionUtils.isEmpty(parent.getChildren())) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(vo);
                }
            }
        }
        long endTime = System.currentTimeMillis();
        log.info("查询地市树业务,耗时:{}ms", endTime-startTime);
        log.debug("查询地市树业务,areaTree={}", areaTree);
        return areaTree;
    }

    /**
     * 递归查询子地区列表并设置到父地区VO中
     * @param parent 父地区VO
     */
    private void queryChildList(AreaVO parent){
        Integer parentId = parent.getId().intValue();
        List<AreaVO> children = queryChildren(parentId);
        parent.setChildren(children);
        if (!CollectionUtils.isEmpty(children)) {
            for (AreaVO child : children) {
                queryChildList(child);
                if (CollectionUtils.isEmpty(child.getChildren())) {
                    break;
                }
            }
        }
    }
}
